﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Threading;
using System.Windows;
using System.Windows.Data;
using System;
using System.Windows.Media.Animation;
using AnimationAdditions;

namespace ItemsAnimation
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : INotifyPropertyChanged
	{
		private List<BusinessObject> items;
		private Random random;

		public PreviewObservableCollection<BusinessObject> Items { get; private set; }

		private int itemSize;
		public int ItemSize
		{
			get
			{
				return itemSize;
			}
			set
			{
				itemSize = value;
				OnPropertyChanged("ItemSize");
			}
		}


		public MainWindow()
		{
			random = new Random(15487);
			ItemSize = 110;
			items = new List<BusinessObject>();
			items.Add(new BusinessObject
					  {
						  FirstName = "First",
						  LastName = "Name",
						  ImagePath = @"Images\01.jpg"
					  });

			items.Add(new BusinessObject
					  {
						  FirstName = "Last",
						  LastName = "Name",
						  ImagePath = @"Images\02.jpg"
					  });

			items.Add(new BusinessObject
					  {
						  FirstName = "Dmirty",
						  LastName = "First",
						  ImagePath = @"Images\03.jpg"
					  });

			items.Add(new BusinessObject
					  {
						  FirstName = "Vladimir",
						  LastName = "Last",
						  ImagePath = @"Images\04.jpg"
					  });

			items.Add(new BusinessObject
					  {
						  FirstName = "Ivan",
						  LastName = "Petrov",
						  ImagePath = @"Images\05.jpg"
					  });

			items.Add(new BusinessObject
					  {
						  FirstName = "Vasya",
						  LastName = "Belov",
						  ImagePath = @"Images\06.jpg"
					  });

			items.Add(new BusinessObject
					  {
						  FirstName = "Gomer",
						  LastName = "Simpson",
						  ImagePath = @"Images\07.jpg"
					  });

			items.Add(new BusinessObject
					  {
						  FirstName = "Dmitry",
						  LastName = "Boizov",
						  ImagePath = @"Images\08.jpg"
					  });

			items.Add(new BusinessObject
					  {
						  FirstName = "VeryVeryLongLongFirstName",
						  LastName = "Smith",
						  ImagePath = @"Images\09.jpg"
					  });

			Items = new PreviewObservableCollection<BusinessObject>(Dispatcher, items);

			InitializeComponent();
		}

		private readonly List<SortDescription> sorts = new List<SortDescription>(new[]
		{
			new SortDescription("LastName", ListSortDirection.Ascending),
			new SortDescription("FirstName", ListSortDirection.Ascending),
			new SortDescription("LastName", ListSortDirection.Descending),
			new SortDescription("FirstName", ListSortDirection.Descending),
		});

		private int indexSort;
		private void SortButton_Click(object sender, RoutedEventArgs e)
		{
			var r = (CollectionViewSource)FindResource("objectCollection");
			r.SortDescriptions.Clear();

			r.SortDescriptions.Add(sorts[indexSort]);
			indexSort++;
			if (indexSort >= sorts.Count)
			{
				indexSort = 0;
			}
		}

		private void Add_Click(object sender, RoutedEventArgs e)
		{
			var nextImageNumber = random.Next(1, 12);
			var nextImage = nextImageNumber < 10
				? "0" + nextImageNumber.ToString()
				: nextImageNumber.ToString();
			Items.Add(new BusinessObject
			{
				FirstName = "Good",
				LastName = "Man",
				ImagePath = string.Format(@"Images\{0}.jpg", nextImage)
			});
		}

		private void Remove_Click(object sender, RoutedEventArgs e)
		{
			Items.RemoveAt(0);
			var collectionViewSource = (CollectionViewSource)FindResource("objectCollection");
		collectionViewSource.View.Refresh();
		}


		//public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj)
		//    where T : DependencyObject
		//{
		//    if (depObj != null)
		//    {
		//        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
		//        {
		//            DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
		//            if (child != null && child is T)
		//            {
		//                yield return (T)child;
		//            }

		//            foreach (T childOfChild in FindVisualChildren<T>(child))
		//            {
		//                yield return childOfChild;
		//            }
		//        }
		//    }
		//}

		public event PropertyChangedEventHandler PropertyChanged;

		private void OnPropertyChanged(string propertyName)
		{
			PropertyChangedEventHandler changed = PropertyChanged;
			if (changed != null) changed(this, new PropertyChangedEventArgs(propertyName));
		}
	}
}
